﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace SensorMonitor
{
    class ComSensor
    {

        
//串口数据到达时的事件
        private void SensorDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //关键 代理
            myDelegate md = new myDelegate(SensorRespondTo);

            int iLen,iLen2;
            try
            {
                if (sensor.IsOpen == true)
                {
                    iLen2 = sensor.ReadBufferSize;

                    byte[] readBuffer = new byte[sensor.ReadBufferSize];

                    iLen = readBuffer.Length;
                    
                    sensor.Read(readBuffer, 0, readBuffer.Length);
                    string inst = Encoding.UTF8.GetString(readBuffer);

                    Invoke(md, inst);
                    //RespondTo(inst);
                }
            }
            catch (Exception err)
            {
                throw err;
            }
        }

//响应指令
        private void SensorRespondTo(string sInst)
        {
            //sInst = sInst.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
            
            int i = sInst.IndexOf("\r");
            sInst = sInst.Substring(0, i); 
            
            i = sInst.IndexOf("NI");
            if (i == 0) { 
                RespondTo_NI(sInst);
                return;
            }

            /*
            //i = sInst.IndexOf("SET");
            //if (i == 0)
            //{
            //    RespondTo_SET(sInst);
            //    return;
            //}
            return;
        }

    }
}
